<?php


namespace app\service;

use app\model\Account;
use think\db\exception\DataNotFoundException;

use think\facade\Session;
use think\response\Json;
use think\captcha\facade\Captcha;


class accountService
{

    public static function resign(array $user): Json
    {
        $result = new Account();
        // 查询用户存在
        try {
            $exit = $result->where('userName', $user['userName'])->find();
            if ($exit == null) {
                // 增加用户
                $result->userName = $user['userName'];
                $result->password = $result->hashPassword($user['password']);
                $result->save();


                $array = [
                    'code' => 200,
                    'msg' => '注册成功'
                ];

            } else {
                $array = [
                    'code' => 500,
                    'msg' => '用户已存在'
                ];

            }
            return json($array, 200);

        } catch (\Exception $err) {
            return json($err->getMessage());
        }


    }

    //登陆
    public static function login(array $data): Json
    {

        $user = new Account();

        try {
//            // 验证 验证码
//            if (!captcha_check($data['code'])) {
//                return json(['code' => 500, 'msg' => '验证码错误']);
//            }

            //查询密码是否正确
            $result = $user->where('userName', $data['userName'])->find()->toArray();
            if ($user->verifyPas($data['password'], $result['password'])) {
                //返回 token
                $token = signToken($result['id']);
                $array = [
                    'code' => 200,
                    'data' => [
                        'token' => $token
                    ],
                    'msg' => '登陆成功'
                ];
            } else {
                $array = [
                    'code' => 500,
                    'msg' => '密码错误'
                ];
            }

            return json($array, 200);

        } catch (\Exception $err) {
            return json(['code' => 500, 'msg' => $err->getMessage()], 500);
        }
    }

    //用户信息
    public static function userInfo($uid): Json
    {

        try {
            $user = Account::find($uid);

            if ($user === null) {
                return \json(['msg' => '用户不存在', 'code' => 500], 200);
            }
            return \json(['data' => $user, 'code' => 200], 200);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }

    }

    // 验证码
    public static function getVerify(): Json
    {
        $verify = Captcha::create();
//        print_r($verify);
        $data = [
            'code' => 200,
            'data' => $verify
        ];

        return \json($data, 200);
    }

    // 用户列表
    public static function getUserList($data, $page): Json
    {
        $user = new Account();

        $list = [];
        if($data) {
           $list = ['username' => $data];
        }

        $list = $user->where($list)->withCount(['userLike', 'userArticle'])->paginate(['page' => $page]);

        return \json(['code' => 200, 'data' => $list], 200);
    }

    //修改
    public static function edit($data): Json
    {
        $userId = $data['uid'];
        try {
            $user = Account::find($userId);
            //更新密码
            $array = [];
            if ($data['oldPassword'] && Account::verifyPas($data['oldPassword'], $user->password)) {
                //加密
                $data['password'] = Account::hashPassword($data['password']);

                $user->save($data);

                $array = [
                    'code' => 200,
                    'msg' => '更新成功'
                ];
            } else {
                $array = [
                    'code' => 500,
                    'msg' => '密码错误'
                ];
            }
            return \json($array, 200);

        } catch (\Exception $e) {
            return \json(['code' => 500, 'msg' => $e->getMessage()]);
        }

    }

}